package com.yeyks.controller;

import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.appcommon.result.ThreadLocalUtil;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.entity.AppAccountInfo;
import com.yeyks.service.AppAccountTokenService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 控制层得到操作人的信息
 * @author: dagny
 * @date: 2019/7/9 0009 10:04
 */
public class BaseController {


    @Autowired
    private AppAccountTokenService tokenService;

    /**
     * 验证token信息 --》 兼容老版本
     *
     * @param token    有效token
     * @param version  版本号
     * @param platform 设备
     * @return 返回账户DTO
     */
    protected AppAccountDTO validToken(String token, String version, String platform) {

        AppAccountDTO appAccountDTO = ThreadLocalUtil.getAccount();
        if (null == appAccountDTO) {
            //验证token
            CommonResult<AppAccountInfo> commonResult = tokenService.getAvailAccount(token,
                    version, platform);
            if (null == commonResult.getData()) {
                throw new OftenFiledException(commonResult.getMsg(), commonResult.getCode());
            }
            appAccountDTO = MyBeanUtils.convert(commonResult.getData(), AppAccountDTO.class);
        }
        if(appAccountDTO.getManagerId() != null){
            appAccountDTO.setId(appAccountDTO.getManagerId());
        }
        return appAccountDTO;
    }
}
